Unix Power Tools, Third Edition chapter
excerpt
By Shelley
Powers, Jerry Peek, Tim O'Reilly, Mike Loukides
Saving Time on the Command Line
28.1. What’s
Special About the Unix Command Line
One of Unix’s best features is the shell’s command
line. Why? Nearly every modern operating system has a command
line; we don’t use card readers with obscure job setup
cards any more. What makes Unix’s special?The Unix shell
command line of shortcuts. Some of these you’ll find
in other operating systems; some you won’t. In this
chapter, we’ll introduce a lot of these shortcuts. Among
other things, we’ll discuss:
· How to run commands more than once (28.8).
· Filename completion (28.6, 28.7), which allows you to type
the beginning of a filename and let the shell fill in the
rest. (This is finally possible on certain Redmond-born OSes
as well, but it usually involves a registry hack or two.)
· Command substitution (28.14), which lets you use the output
from one command as arguments to another. (Note that this
is different from pipelining.)
· Process substitution in bash, and a script named ! for other
shells, lets you put the output of a command into a temporary
file and give that filename to a process.
· The ability to repeat commands with various methods (28.10,
28.11).
· Handling of command lines that become too long (28.17).
Some fundamental command-line features that we aren’t
discussing in this chapter, but which are discussed elsewhere,
are:
· Job control (23.3), which lets you run several commands
at the same time.
· Aliases (29.2), or abbreviations, for commands. Shell functions
(29.11) are similar.
· Command-line editing (30.14) and history substitution (30.8).
These are two different ways (both useful) to “recall”
previous commands.
· Quoting (27.12, 27.13), the way you “protect”
special characters from the Unix shell.
· Wildcards (33.2).
You don’t need to be a command-line virtuoso to use
Unix effectively. But you’d be surprised at how much
you can do with a few tricks. If all you can do at the command
line is type ls or start Mozilla or the Gimp, you’re
missing out on a lot.
--ML
28.2. Reprinting
Your Command Line with CTRL-r
You’re logged in from home, running a program and answering
a prompt. As you’re almost done, modem noise prints
xDxD@! on your screen. Where were you? Or you’re typing
a long command line and a friend interrupts you with write
(1.21) to say it’s time for lunch. Do you have to press
CTRL-u and start typing all over again?
If your system understands the rprnt character (usually set
to CTRL-r), you can ask for the command line to be reprinted
as it was. In fact, you can use CTRL-r any time you want to
know what the system thinks you’ve typed on the current
line—not just when you’re interrupted. But this
only works in the normal cooked input mode; programs like
vi that do their own input processing may treat CTRL-r differently.
Here’s an example:
% egrep '(10394|29433|49401)' /work/symtower/
Message from alison@ruby on ttyp2 at
12:02 ...
how about lunch?
EOF
CTRL-r
egrep '(10394|29433|49401)' /work/symtower/logs/*
After the interruption, I just pressed CTRL-r. It reprinted
the stuff I’d started typing. I finished typing and
pressed RETURN to run it.
If you use a shell like the Korn shell that has interactive
command editing, you can probably use it to reprint the command
line, too. In bash and other commands that use the readline
file, though, from vi editing mode, CTRL-r still seems to
start an Emacs-style reverse search. So I added this fix to
my ~/.inputrc file:
set editing-mode vi
# By default, in vi text-input mode,
^R does Emacs "reverse-i-search".
# In command mode, you can use the vi command ^L to redraw
the line.
# Fix it in text-input mode:
"\\C-r": redraw-current-line
--JP
28.3. Use Wildcards
to Create Files?
The shells’ [] (square bracket) wildcards will match
a range of files. For instance, if you have files named afile,
bfile, cfile, and dfile, you can print the first three by
typing:
% lpr [a-c]file
Now, let’s say that you want to create some more files
called efile, ffile, gfile, and hfile. What’s wrong
with typing the command line below? Try it. Instead of vi,
you can use your favorite editor or the touch (14.8) command:
% vi [e-h]file Doesn't make those four
files
% ls
afile bfile cfile dfile
Stumped? Take a look at article 1.13 about wildcard matching.
The answer: wildcards can’t match names that don’t
exist yet. That’s especially true with a command like
touch ?file (14.8) or touch *file—think how many filenames
those wildcards could possibly create!
Article 28.4 explains shell {_ } operators that solve this
problem. And, by the way, if you just created one new file
named [e-h]file, simply quote (27.12) its name to remove it:
rm "[e-h]file"
--JP